//
//  requestManage.m
//  XYMaterial
//
//  Created by 肖扬 on 15/12/12.
//  Copyright © 2015年 肖扬. All rights reserved.
//

#import "requestManage.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
@implementation requestManage
@synthesize isRequestFinish;
@synthesize postData;
-(void)dealloc
{
    self.postData = nil;
    self.request = nil;
}

- (id)init
{
    self = [super init];
    
    if (self) {
        self.timeOutSecond = 20;
    }
    return self;
}

-(NSData *)get:(NSString *)url method:(NSString *)method body:(NSString *)body error:(NSError **)error
{
    return [self.request responseData];
}


-(NSString *)get:(NSString *)url error:(NSError **)error;
{
    
#ifdef DEBUG
    NSLog(@"request get url %@",url);
#endif
    
    NSURL *nsUrl = [NSURL URLWithString:url];
    self.request = nil;
    
    self.request = [ASIHTTPRequest requestWithURL:nsUrl];
    [self.request setTimeOutSeconds:self.timeOutSecond];
    self.request.delegate = self;
    self.isRequestFinish = NO;
    
    [self.request startAsynchronous];
    
    while( !self.isRequestFinish)
    {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
    
    if ([self.request error])
    {
        *error = [self.request error];
        
        return nil;
    }
    
    NSData *responseData = [self.request responseData];
    
    NSString *retStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    
    if (retStr == nil)
    {
        retStr =  [[NSString alloc] initWithData:responseData encoding:NSUnicodeStringEncoding];
    }
    
#ifdef DEBUG
    NSLog(@"str %@",retStr);
#endif
    return retStr;
}

-(NSString *)post:(NSString *)url  postDic:(NSDictionary *)dic error:(NSError **)error;
{
    
#ifdef DEBUG
    NSLog(@"request post url %@",url);
#endif
    
    NSURL *nsUrl = [NSURL URLWithString:url];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:nsUrl];
    [request setDelegate:self];
    
    for(id key in [dic allKeys])
    {
        [request setPostValue:[self.postData objectForKey:key] forKey:(NSString *)key];
    }
    
    [request buildPostBody];
    
#ifdef DEBUG
    NSString * str = [[NSString alloc] initWithData:[request postBody] encoding:NSUTF8StringEncoding];
    NSLog(@"str %@",str);
#endif
    
    [request startAsynchronous];
    
    self.isRequestFinish = NO;
    while( !self.isRequestFinish )
    {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
    
    if ([request error])
    {
        *error = [request error];
        
        return nil;
    }
    
    return [[NSString alloc] initWithData:[request responseData] encoding:NSUTF8StringEncoding];
    
    return nil;
}


-(NSString *)post:(NSString *)url  postDataDic:(NSMutableData *)dic error:(NSError **)error;
{
    
#ifdef DEBUG
    NSLog(@"request post url %@",url);
#endif
    
    NSURL *nsUrl = [NSURL URLWithString:url];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:nsUrl];
    [request setDelegate:self];
    
//    for(id key in [data allKeys])
//    {
//        [request setPostValue:[self.postData objectForKey:key] forKey:(NSString *)key];
//    }
//    
//    [request buildPostBody];
    if ([NSJSONSerialization isValidJSONObject:dic])
    {
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error: nil];
        NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];
//        NSError *error1 = [request error];
        [request setPostBody:tempJsonData];
    }

    
//#ifdef DEBUG
    NSString * str = [[NSString alloc] initWithData:[request postBody] encoding:NSUTF8StringEncoding];
    NSLog(@"str %@",str);
//#endif
    
    [request startAsynchronous];
    [MBProgressHUD showHUD];
    self.isRequestFinish = NO;
    while( !self.isRequestFinish )
    {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }
    
    if ([request error])
    {
//        *error = [request error];
        
        return nil;
    }
    [MBProgressHUD dissmiss];
    return request.responseString;
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    self.isRequestFinish = YES;
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
    self.isRequestFinish = YES;
}

- (void)cancel
{
    [self.request cancel];
}

@end
